package com.pucong.gitee.database.backup.business.database.controller;

import com.pucong.gitee.database.backup.business.database.service.AutoDataSourceService;
import com.pucong.gitee.database.backup.common.entity.database.AutoDataSource;
import com.pucong.gitee.database.backup.common.entity.database.dto.AutoDataSourceSaveDto;
import com.pucong.gitee.database.backup.common.entity.database.dto.AutoDataSourceTestConnectDto;
import com.pucong.gitee.database.backup.common.entity.database.dto.RenameDataSourceDto;
import com.pucong.gitee.database.backup.common.entity.database.dto.SelectDbBackTreeDto;
import com.pucong.gitee.database.backup.common.entity.database.vo.DbBackTreeVo;
import com.pucong.gitee.database.backup.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author pucong
 * @url https://gitee.com/pucong
 * @create 2020-06-23
 */
@RestController
@RequestMapping("/auto/dataSource")
@Api(value = "数据源信息-API ", tags = "1. 数据库信息表 API")
public class AutoDataSourceController {

    @Autowired
    private AutoDataSourceService autoDataSourceService;

    @GetMapping("/getSourceInfoById/{id}")
    @ApiOperation(value = "102根据主键查询", notes = "根据主键查询")
    public Res<AutoDataSource> getBaseInfoById(@ApiParam("主键")@PathVariable("id") String id){
        AutoDataSource autoDataSource = autoDataSourceService.getBaseInfoById(id);
        return Res.ok(autoDataSource);
    }

    @PostMapping("/saveSourceInfo")
    @ApiOperation(value = "103新增和修改数据库信息")
    public Res<Integer> addBaseInfo(@Validated @RequestBody AutoDataSourceSaveDto dto){
        int i = autoDataSourceService.saveBaseInfo(dto);
        return Res.ok(i);
    }

    @GetMapping("/getTreeList")
    @ApiOperation(value = "104根据上级 查询下级树数据")
    public Res<List<DbBackTreeVo>> getTreeList(SelectDbBackTreeDto dto){
        List<DbBackTreeVo> databaseTreeList = autoDataSourceService.getTreeList(dto);
        return Res.ok(databaseTreeList);
    }

    @DeleteMapping("/deleteTree")
    @ApiOperation(value = "104根据上级 查询下级树数据")
    public Res deleteTree(SelectDbBackTreeDto dto){
        autoDataSourceService.deleteTree(dto);
        return Res.ok();
    }

    @PostMapping("/connectTest")
    @ApiOperation(value = "连接测试")
    public Res<Boolean> connectTest(@Validated @RequestBody AutoDataSourceTestConnectDto dto){
        Boolean i = autoDataSourceService.connectTest(dto);
        return Res.ok(i);
    }

    @PostMapping("/rename")
    @ApiOperation(value = "重命名")
    public Res<Boolean> rename(@Validated @RequestBody RenameDataSourceDto renameDataSourceDto){
        Boolean aBoolean = autoDataSourceService.rename(renameDataSourceDto);
        return Res.ok(aBoolean);
    }
}
